/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.LoaiSanPhamDao;
import dao.NhaSanXuatDao;
import dao.SanPhamDao;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import pojo.Loaisanpham;
import pojo.Nhasanxuat;
import pojo.Sanpham;

/**
 *
 * @author Alvis
 */
public class Admin_ProductController {

    /** Creates a new instance of Admin_ProductController */
    private Sanpham[] listSanPham;
    private Sanpham sanPhamTemp = new Sanpham();
    private Loaisanpham[] listLoaiSanPham ;
    private Nhasanxuat[] listNhaSanXuat;
    private List<String> loaiSanPhams = new ArrayList<String>();
    private List<String> nhaSanXuats = new ArrayList<String>();
    
    public Admin_ProductController() {
        sanPhamTemp.setNhasanxuat(new Nhasanxuat());
        sanPhamTemp.setLoaisanpham(new Loaisanpham());
        LoaiSanPhamDao lspDAO = new LoaiSanPhamDao();
        NhaSanXuatDao nsxDAO = new NhaSanXuatDao();
        listLoaiSanPham = lspDAO.getAllProductType();
        listNhaSanXuat = nsxDAO.getAllProducer();
        loaiSanPhams.add("");
        nhaSanXuats.add("");
        for(int i=0;i<listLoaiSanPham.length;i++)
        {
            loaiSanPhams.add(listLoaiSanPham[i].getTenloaisanpham());
        }
        for(int i=0;i<listNhaSanXuat.length;i++)
        {
            nhaSanXuats.add(listNhaSanXuat[i].getTennhasanxuat());
        }
        getProductList();
        
    }

    /**
     * @return the sanPhamTemp
     */
    public Sanpham getSanPhamTemp() {
        return sanPhamTemp;
    }

    /**
     * @param sanPhamTemp the sanPhamTemp to set
     */
    public void setSanPhamTemp(Sanpham sanPhamTemp) {
        this.sanPhamTemp = sanPhamTemp;
    }
    
    public String InsertProduct()
    {
        SanPhamDao sanPhamDAO = new SanPhamDao();
        LoaiSanPhamDao lspDAO = new LoaiSanPhamDao();
        NhaSanXuatDao nsxDAO = new NhaSanXuatDao();
        Sanpham value = new Sanpham();
        Nhasanxuat nhasanxuat = new Nhasanxuat();
        if(sanPhamTemp.getNhasanxuat().getTennhasanxuat().compareTo("")!=0)
        nhasanxuat=nsxDAO.getProducerByName(sanPhamTemp.getNhasanxuat().getTennhasanxuat());
        Loaisanpham loaisanpham = new Loaisanpham();
        if(sanPhamTemp.getLoaisanpham().getTenloaisanpham().compareTo("")!=0)
            loaisanpham = lspDAO.getProductTypeByName(sanPhamTemp.getLoaisanpham().getTenloaisanpham());
        
        value.setTensanpham(sanPhamTemp.getTensanpham());
        value.setXuatxu(sanPhamTemp.getXuatxu());
        value.setGiaban(sanPhamTemp.getGiaban());
        value.setLoaisanpham(loaisanpham);
        value.setMota(sanPhamTemp.getMota());
        value.setNhasanxuat(nhasanxuat);
        value.setSoluotban(sanPhamTemp.getSoluotban());
        value.setSoluotxem(sanPhamTemp.getSoluotxem());
        sanPhamDAO.Insert(value);
        return "indexproduct";
    }
    
     public String UpdateProduct()
    {
        SanPhamDao sanPhamDAO = new SanPhamDao();
        LoaiSanPhamDao lspDAO = new LoaiSanPhamDao();
        NhaSanXuatDao nsxDAO = new NhaSanXuatDao();
        Sanpham value = new Sanpham();
        Nhasanxuat nhasanxuat = new Nhasanxuat();
        nhasanxuat=nsxDAO.getProducerByName(sanPhamTemp.getNhasanxuat().getTennhasanxuat());
        Loaisanpham loaisanpham = new Loaisanpham();
        loaisanpham = lspDAO.getProductTypeByName(sanPhamTemp.getLoaisanpham().getTenloaisanpham());
        
        value.setTensanpham(sanPhamTemp.getTensanpham());
        value.setXuatxu(sanPhamTemp.getXuatxu());
        value.setGiaban(sanPhamTemp.getGiaban());
        value.setLoaisanpham(loaisanpham);
        value.setMota(sanPhamTemp.getMota());
        value.setNhasanxuat(nhasanxuat);
        value.setSoluotban(sanPhamTemp.getSoluotban());
        value.setSoluotxem(sanPhamTemp.getSoluotxem());
        sanPhamDAO.Update(value,sanPhamTemp.getMasanpham());
        return "indexproduct";
    }
     public String DeleteProduct()
    {
        SanPhamDao sanPhamDAO = new SanPhamDao();
       
        sanPhamDAO.Delete(sanPhamTemp.getMasanpham());
        return "indexproduct";
    }
     public String getProductUpdate() {
        SanPhamDao sanPhamDAO = new SanPhamDao();
        setSanPhamTemp(sanPhamDAO.getProduct(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("txtID").toString())));
        return "updateproduct";

    }
    public String getProductDelete() {
        SanPhamDao sanPhamDAO = new SanPhamDao();
        setSanPhamTemp(sanPhamDAO.getProduct(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("txtID").toString())));
        return "deleteproduct";

    }
    public String getProductList()
        {
        try {
            SanPhamDao sanPhamDAO = new SanPhamDao();
            listSanPham = sanPhamDAO.getAllProduct();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            
        }
        return "indexproduct";

    }
    public String SearchProduct() {
        String search =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("searchstring").toString();
        try {
            SanPhamDao sanPhamDAO = new SanPhamDao();
            if(search.equals(""))
             listSanPham = sanPhamDAO.getAllProduct();
            else
                listSanPham = sanPhamDAO.SearchProduct(search);
                 
            

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            
        }
        return "indexproduct";

    }
    /**
     * @return the loaiSanPhams
     */
    public List<String> getLoaiSanPhams() {
        return loaiSanPhams;
    }

    /**
     * @return the nhaSanXuats
     */
    public List<String> getNhaSanXuats() {
        return nhaSanXuats;
    }

    /**
     * @return the listSanPham
     */
    public Sanpham[] getListSanPham() {
        return listSanPham;
    }

    /**
     * @return the listLoaiSanPham
     */
 
}
